<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>优惠券新增</title>
<%@include file="../include/include.jsp"%>
<link rel="stylesheet" type="text/css" href="${csspath }/public.css" />
<script type="text/javascript" src="${jspath}/jquery/jquery-1.8.3.js"></script>
<link rel="stylesheet" type="text/css"
	href="${bootstrapPath}/css/bootstrap.css" />
<script type="text/javascript" src="${bootstrapPath}/js/bootstrap.js"></script>
<script type="text/javascript"
	src="${bootstrapPath}/js/bootstrap3-validation.js"></script>
<script type="text/javascript" src="${jspath}/jquery/jquery.alerts.js"></script>

</head>

<script type="text/javascript">
	var row_count = 0;
	$(function() {
		//新增行
		$("#insertRow")
				.click(
						function() {
							var table = $("#data_tabs");
							var row = $("<tr></tr>");
							var td0 = $("<td></td>");
							var td1 = $("<td></td>");
							var td2 = $("<td style='border-top: 1px solid #fff;'></td>");
							td0
									.append($("<input class='form-control input-sm' id='itemName"+row_count+"' type='text' />"));
							row.append(td0);

							td1
									.append($("<input class='form-control input-sm text-right' id='quantity"
											+ row_count
											+ "' type='text'/>"));
							row.append(td1);

							td2
									.append($("<button type='button' id='delete"
											+ row_count
											+ "' class='btn btn-warning btn-sm' onclick='deleteRow(this.id)' style='margin-left: 10px;'>删除</button>  "));
							row.append(td2);

							table.append(row);
							row_count++;
							changeQuantity();
						});
		function changeQuantity() {
			if (row_count >= 1) {
				for ( var k = 0; k < row_count; k++) {
					if ($("#quantity" + k).val() == null
							|| $("#quantity" + k).val() == "") {
						$("#quantity" + k).val("1");
					}
				}
			}
		}

		$("#defineTicketForm").validation({
			icon : true
		});

		//新增优惠券信息
		$("#saveBtn")
				.click(
						function() {
							if ($("#defineTicketForm").valid(this, '内容出错') == false) {
								return false;
							}
							if (check() == false) {
								jAlert('填写信息不完整', '提示');
								return false;
							}
							var ticketName = $("#ticketName").val();
							var deductionMethod = $("#deductionMethod").val();
							var deductionAmount = $("#deductionAmount").val();
							var verificationMethod = $("#verificationMethod")
									.val();
							var itemName = $("#itemName").val();
							var quantity = $("#quantity").val();

							var dataStr = itemName + "@#" + quantity + "###";
							if (row_count >= 1) {
								for ( var k = 0; k < row_count; k++) {
									var quantity2 = $("#quantity" + k).val();
									if (quantity2 != undefined
											&& quantity2 != null
											&& quantity2 != "") {
										var itemNames = $("#itemName" + k)
												.val();
										var quantitys = $("#quantity" + k)
												.val();

										dataStr = dataStr + itemNames + "@#"
												+ quantitys + "###";
									}
								}
							}
							var radio = document.getElementsByName("smsRemind");
							var smsRemind = null;
							for ( var i = 0; i < radio.length; i++) {
								if (radio[i].checked == true) {
									smsRemind = radio[i].value;
									break;
								}
							}
							$
									.ajax({
										url : '${webcontext}/crmdp/dealer/coupon/saveCoupon',
										type : 'post',
										data : {
											'ticketName' : ticketName,
											'deductionMethod' : deductionMethod,
											'deductionAmount' : deductionAmount,
											'verificationMethod' : verificationMethod,
											'smsRemind':smsRemind,
											'dataStr' : dataStr
										},
										success : function(data) {
											if (data.code == true) {
												jAlert(
														data.msg,
														"提示",
														function() {
															window.location = '${webcontext}/crmdp/dealer/coupon/toDefineTicKet';
														});
											} else if (data.code == false) {
												jAlert(data.msg, "提示");
											}
										},
										error : function() {
											jAlert("服务器错误，请联系管理员", "提示");
										},
									});
						});
		//返回
		$("#returnBtn")
				.click(
						function() {
							window.location.href = "${webcontext}/crmdp/dealer/coupon/toDefineTicKet";
						});

	});

	function check() {
		if (row_count >= 1) {
			for ( var k = 0; k < row_count; k++) {
				var itemName = $("#itemName" + k).val();
				var quantity = $("#quantity" + k).val();
				if (itemName != undefined && itemName != null && itemName != "") {
					if (itemName == null || itemName == "") {
						return false;
					}
					if (quantity == null || quantity == "") {
						return false;
					}
				}else{
					if (itemName == null || itemName == "") {
						return false;
					}
				}
			}
			var itemName = $("#itemName").val();
			var quantity = $("#quantity").val();
			if (itemName == null || itemName == "") {
				return false;
			}
			if (quantity == null || quantity == "") {
				return false;
			}
		} else {
			var itemName = $("#itemName").val();
			var quantity = $("#quantity").val();
			if (itemName == null || itemName == "") {
				return false;
			}
			if (quantity == null || quantity == "") {
				return false;
			}
		}
		return true;
	}

	//删除行
	function deleteRow(id) {
		$("#" + id).parent().parent().remove();
	}
</script>

<style>
.hr {
	text-align: center;
	width: 100%;
	background-color: #0071C1;
	height: 1px;
	margin-bottom: 10px;
}
.rowDiv {
	width: 90%;
}

.formDiv {
	float: left;
	width: 50%;
}
</style>
<body>
	<div id="content">
		<div style="color: #025D7B; height: 26px; line-height: 26px;">
			<span>优惠券定义</span> <span>&gt;</span> <span>优惠券新增</span>
		</div>
		<div class="hr"></div>
		<form:form id="defineTicketForm" action="" class="form-horizontal"
			method="post" modelAttribute="dto">
			<div class="row rowDiv">
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="ticketName">名称</label>
					<div class="col-sm-6">
						<input class="form-control input-sm" id="ticketName" type="text"
							check-type="required" required-message="请填写名称" />
					</div>
				</div>
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="maximunTimes">最大使用次数 </label>
					<div class="col-sm-6">
						<input class="form-control input-sm" id="maximunTimes"
							name="maximunTimes" type="text" value="1"
							check-type="required" required-message="最大使用次数" />
					</div>
				</div>
			</div>
			<div class="row rowDiv">
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="deductionMethod">抵扣方式</label>
					<div class="col-sm-6">
						<select id="deductionMethod" class="form-control input-sm">
							<option value="AMOUNT">金额</option>
							<option value="PERCENT">百分比</option>
						</select>
					</div>
				</div>
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="deductionAmount">抵扣值</label>
					<div class="col-sm-6">
						<input class="form-control input-sm text-right"
							id="deductionAmount" type="text" check-type="number" />
					</div>
				</div>
			</div>
			<div class="row rowDiv">
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="verificationMethod">抵扣凭证</label>
					<div class="col-sm-6">
						<select id="verificationMethod" class="form-control input-sm">
							<option value="CODE">抵扣码</option>
							<option value="N/A">无</option>
						</select>
					</div>
				</div>
				<div class="form-group formDiv">
					<label class="col-sm-3 control-label" for="smsRemind">是否发送使用短信</label>
					<label class="radio-inline"> <input type="radio"
						name="smsRemind" id="smsRemind1" value="1">是
					</label> <label class="radio-inline"> <input type="radio"
						name="smsRemind" id="smsRemind2" value="0" checked>否
					</label>
				</div>
			</div>
		</form:form>
		<table id="data_tabs" class="table"
			style="width: 70%; margin-top: 20px;">
			<thead>
				<tr>
					<th class="text-center">产品说明</th>
					<th class="text-center">数量</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td><input class="form-control input-sm" id="itemName"
						type="text" /></td>
					<td><input class="form-control input-sm text-right"
						id="quantity" type="text" value="1" /></td>
				</tr>
			</tbody>
		</table>
		<div style="margin-left: 300px;">
			<button type="button" class="btn btn-success btn-sm" id="saveBtn">
				<i class="glyphicon glyphicon-floppy-saved" style="color: #FFFFFF"></i>&nbsp;保
				存
			</button>
			<button type="button" class="btn btn-success btn-sm" id="insertRow">
				<i class="glyphicon glyphicon-plus" style="color: #FFFFFF"></i>&nbsp;新
				增
			</button>
			<button type="button" class="btn btn-success btn-sm" id="returnBtn">
				<i class="glyphicon glyphicon-retweet" style="color: #FFFFFF"></i>&nbsp;返回
			</button>
		</div>
	</div>
</body>
</html>